package cn.danger.com.fragment.memo;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import cn.danger.com.activity.CommonActivity;
import cn.danger.com.activity.R;
import cn.danger.com.fragment.BackHandledFragment;
import cn.danger.com.model.entity.Memo;
import cn.danger.com.presenter.MemoPresenter;
import cn.danger.com.presenter.MemoPresenterImpl;
import cn.danger.com.util.Constants;
import cn.danger.com.util.DateUtil;
import cn.danger.com.view.CommonView;

/**
 * Created by 俊 on 2017/1/1.
 */
public class MemoDetailFragment extends BackHandledFragment implements View.OnClickListener, CommonView {

    private boolean hadIntercept;
    private View view;
    private CommonActivity activity;
    private Toolbar toolbar;
    private TextView title_textView;
    private TextView date_textView;
    private TextView content_textView;
    private FloatingActionButton add_button;
    private Memo memo;
    private ProgressDialog dialog;
    private MemoPresenter presenter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        view = inflater.inflate(R.layout.fragment_memo_detail, container, false);

        bindView();
        bindEvent();

        return view;
    }

    @Override
    public void bindView() {
        activity = (CommonActivity) getActivity();
        toolbar = (Toolbar) view.findViewById(R.id.toolbar);
        toolbar.setTitle("");
        activity.setSupportActionBar(toolbar);

        title_textView = (TextView) view.findViewById(R.id.title_textView);
        date_textView = (TextView) view.findViewById(R.id.date_textView);
        content_textView = (TextView) view.findViewById(R.id.content_textView);
        add_button = (FloatingActionButton) view.findViewById(R.id.add_button);

        dialog = new ProgressDialog(activity, ProgressDialog.THEME_HOLO_LIGHT);
        dialog.setMessage("正在执行...");
        dialog.setCanceledOnTouchOutside(false);

        memo = (Memo) getArguments().getSerializable("memo");

        title_textView.setText(memo.getTitle());
        date_textView.setText(DateUtil.getDate(memo.getDate()));
        content_textView.setText("\u3000\u3000" + memo.getContent());
        presenter = new MemoPresenterImpl(activity, this);

    }

    @Override
    public void bindEvent() {
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                activity.finish();
            }
        });
        add_button.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.add_button:
                AddMemoFragment fragment = new AddMemoFragment();
                Bundle bundle = new Bundle();
                bundle.putString("origin", "detail");
                fragment.setArguments(bundle);
                getFragmentManager().beginTransaction().addToBackStack(null).replace(R.id.container, fragment).commit();
                break;
            default:
                break;
        }
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        inflater.inflate(R.menu.detail_memu, menu);
        super.onCreateOptionsMenu(menu, inflater);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == R.id.edit) {
            AddMemoFragment fragment = new AddMemoFragment();
            Bundle bundle = new Bundle();
            bundle.putSerializable("memo", memo);
            fragment.setArguments(bundle);
            getFragmentManager().beginTransaction().addToBackStack(null).replace(R.id.container, fragment).commit();
        } else if (item.getItemId() == R.id.delete) {
            delete();
        }
        return super.onOptionsItemSelected(item);
    }

    private void delete() {
        AlertDialog.Builder builder = new AlertDialog.Builder(activity);
        builder.setTitle("提示");
        builder.setMessage("您确定要删除这条备忘录吗？");
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                presenter.deleteMemo(memo);
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        });
        builder.create().show();
    }

    @Override
    public void showDialog() {
        dialog.show();
    }

    @Override
    public void hideDialog() {
        dialog.dismiss();
    }

    @Override
    public void showMessage(String message) {
        Toast.makeText(activity, message, Toast.LENGTH_SHORT).show();
        if (message.equals(Constants.SUCCESS_TO_DELETE_LOCAL) || message.equals(Constants.SUCCESS_TO_DELETE_MEMO)) {
            Intent intent = new Intent("update_memo_list");
            activity.sendBroadcast(intent);
            activity.finish();
        }
    }

    @Override
    public boolean onBackPressed() {
        if (hadIntercept) {
            return false;
        } else {
            hadIntercept = true;
            return true;
        }
    }

}
